<?php

/**
 * history actions.
 *
 * @package    FirstSeec
 * @subpackage history
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class historyActions extends sfActions
{
	//
	//	Asked History
	//
	public function executeShowAsked(sfWebRequest $request) {
//		$userID = $this->getUser()->getAttribute('user_id');
//		if ($userID ==null) {
//			$this->redirect('..//user//loginSuccess');
//		}
	}

	public function executeAskedQuestions(sfWebRequest $request) {
//		$data0 = '{"questions": []}';
//		$data3 = '{"questions": [{"id":"12","date":"Apr 24, 2010","content":"Where are you?","answers": [{"id":"32","date":"Apr 26, 2010","userid":"342","firstname":"Tony","content":"I dont know where I am..."}, {"id":"45","date":"Apr 26, 2010","userid":"322","firstname":"Ynot","content":"I tnod okwn wrehe I ma... ...ma I erehw wonk tnod I"}]}, {"id":"23","date":"Apr 25, 2010","content":"Who are you?","answers": []}, {"id":"90","date":"Apr 25, 2010","content":"How are you?","answers": [{"id":"87","date":"Apr 25, 2010","userid":"1","firstname":"Simon","content":"Not bad, well, actually I dont know what I am saying. It is almost 3 o clock in the morning, my brain stops working... Hope our project could be finished by the demo date. God bless us. "}]}]}';
		if (!$this->getUser()->isAuthenticated()) {
			$data = '{"redirect":"../user/login"}';
			return $this->renderPartial('response', array('json_data' => $data));
		}
				
		$userID = $this->getUser()->getAttribute('user_id');

		$temp = new SeecQuestion();
		$questions = $temp->askedQuestion($userID);
		$questionNum = count($questions);
		
		$formatData = "{\"questions\": [";

		for ($i=0; $i<$questionNum; $i++) {
			$questionID = $questions[$i]["id"];
			$questionDate = $questions[$i]["created_at"];
			$questionContent = $questions[$i]["content"];
			$questionID = $this->checkString($questionID);
			$questionDate = $this->checkString($questionDate);
			$questionContent = $this->checkString($questionContent);

			$formatData .= "{\"id\":\"";
			$formatData .= $questionID;
			$formatData .= "\", \"date\":\"";
			$formatData .= $questionDate;
			$formatData .= "\", \"content\":\"";
			$formatData .= $questionContent;
			$formatData .= "\", \"answers\":[";		
			
			$query = Doctrine_Query::create()
			->select('*')
			->from ('SeecAnswer SA')
			->where('SA.question_id = ?', $questionID)->execute();
				
			$answers = $query->toArray();
			$answerNum = count($answers);
			
			for ($j=0; $j<$answerNum; $j++) {
				$answerID = $answers[$j]["id"];
				$answerDate = $answers[$j]["created_at"];
				$answerUserID = $answers[$j]["user_id"];
				$answerContent = $answers[$j]["content"];
				
				$query = Doctrine_Query::create()
				->select('*')
				->from ('SeecUser SU')
				->where('SU.id = ?', $answerUserID)->execute();
				$users = $query->toArray();
				$answerFirstName = $users[0]["firstname"];
				
				$answerID = $this->checkString($answerID);
				$answerDate = $this->checkString($answerDate);
				$answerUserID = $this->checkString($answerUserID);
				$answerContent = $this->checkString($answerContent);
				$answerFirstName = $this->checkString($answerFirstName);
				
				$formatData .= "{\"id\":\"";
				$formatData .= $answerID;
				$formatData .= "\", \"date\":\"";
				$formatData .= $answerDate;
				$formatData .= "\", \"userid\":\"";
				$formatData .= $answerUserID;
				$formatData .= "\", \"firstname\":\"";
				$formatData .= $answerFirstName;
				$formatData .= "\", \"content\":\"";
				$formatData .= $answerContent;
				$formatData .= "\"},";
			}
			if ($answerNum!==0) {
				$formatData = substr($formatData, 0, -1);
			}
			$formatData .= "]},";
		}
		if ($questionNum!==0) {
			$formatData = substr($formatData, 0, -1);
		}
		$formatData .= "]}";

		return $this->renderPartial('response', array('json_data' => $formatData));
	}

	protected function checkString($input) {
		$input = str_replace("\\","\\\\",$input);
		$input = str_replace("\"","\\\"",$input);
		$input = str_replace("\'","\\\'",$input);
		$input = str_replace("<","&lt;",$input);
		$input = str_replace(">","&gt;",$input);
		return $input;
	}
}
